package common.utils.annotations;

import org.springframework.context.support.EmbeddedValueResolutionSupport;
import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Parser;
import org.springframework.format.Printer;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

@Component
public class DesensitizationAnnotationFormatterFactory extends EmbeddedValueResolutionSupport implements AnnotationFormatterFactory<Desensitization> {

    /**
     * 返回为处理的变量的类型
     *
     * @return
     */
    @Override
    public Set<Class<?>> getFieldTypes() {
        Set<Class<?>> hashSet = new HashSet<>();
        hashSet.add(String.class);
        return hashSet;
    }

    @Override
    public Printer<?> getPrinter(Desensitization annotation, Class<?> aClass) {
        return getFormatter(annotation);
    }

    @Override
    public Parser<?> getParser(Desensitization annotation, Class<?> aClass) {
        return getFormatter(annotation);
    }

    private DesensitizationFormatter getFormatter(Desensitization desensitization) {
        DesensitizationFormatter formatter = new DesensitizationFormatter();
        formatter.setTypeEnum(desensitization.type());
        return formatter;
    }
}
